package com.ly.juc;
import java.util.Collections;
import	java.util.UUID;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.stream.Collectors;

/**
 * @Classname SetUnSafe
 * @Description TODO
 * @Date 2020/10/4 15:20
 * @Author 冷心影翼
 */
public class SetUnSafe {

	public static void main(String[] args) {
//		unsafe();
		collectionsSynchronizedSet();
//		copyOnWriteArraySet();
	}

	public static void unsafe() {
		Set<String> set = new HashSet<>();
		for(int i=0;i<100;i++) {
			int finalI = i;
			new Thread(()->{
				set.add(finalI+"");
			}).start();
			System.out.println(set);
		}
	}

	public static void collectionsSynchronizedSet() {
		Set<String> set = Collections.synchronizedSet(new HashSet<>());
		for(int i=0;i<1000;i++) {
			int finalI = i;
			new Thread(()->{
				set.add(finalI+"");
			}).start();
			System.out.println(set);
		}
	}

	public static void copyOnWriteArraySet() {
		Set<String> set = new CopyOnWriteArraySet<>();
		for(int i=0;i<1000;i++) {
			int finalI = i;
			new Thread(()->{
				set.add(finalI+"");
			}).start();
			System.out.println(set);
		}
	}
}
